/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.database;

import br.com.models.Days;
import br.com.models.Teacher;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Valdigleis
 */
public class DaoTeacherDays {
    
    private String sql;
    
    private Connection connection;
   
    private DaoDays daodays;
    /**
     * Método construtor que implementa a classe
     * DaoTeacherDay, que é responsavel pela comunicação
     * do sistema com a base de dados na tabela de
     * relacionamento Teacher_Days.
     *  
     */
    public DaoTeacherDays() {
         this.connection = Derby.getConnection();
    }
    
    /**
     * Método que grava o expediente do professor.
     * 
     * @param idTEACHER
     * @param day 
     */
    public void salveExpedient(int idTEACHER, ArrayList<Days> day){
        for(int i = 0; i < day.size(); i++){
            saveTeacher_Day(idTEACHER, idDay(day.get(i)));
        }
        closeConnection();
    }
    
    /**
     * Método que encontra o id do dia a ser inserido no
     * expediente do professor.
     * 
     * @param day
     * @return id do dia.
     */
    private int idDay(Days day){
        if(day.getName().equals("Segunda-feira")){
            return 1;
        }
        if(day.getName().equals("Terça-feira")){
            return 2;
        }
        if(day.getName().equals("Quarta-feira")){
            return 3;
        }
        if(day.getName().equals("Quinta-feira")){
            return 4;
        }else{
            return 5;
        }
    }
    
    /**
     * Método responsável que vincula na base de dados o
     * dia de expediente com um determiando professor.
     * 
     * @param idTeacher
     * @param idDay 
     */ 
    private void saveTeacher_Day(int idTeacher, int idDay){
        try{
            sql = "INSERT INTO SAD.TEACHER_DAYS" +
                " (teacher, days)" + " values (?,?)";
            PreparedStatement stmt = connection.prepareStatement(sql);
            stmt.setInt(1, idTeacher);
            stmt.setInt(2, idDay);
            stmt.execute();
            stmt.close();
        }catch(SQLException e){
            System.err.println("Erro ao salvar o expediente!");
            System.out.println(e.getMessage());
        }
    }
    
    
    public ArrayList<Integer> getExpedient(int idTeacher){
        try{
            sql = "SELECT * FROM SAD.TEACHER_DAYS WHERE teacher=?";
            PreparedStatement stmt = connection.prepareStatement(sql);
            stmt.setInt(1, idTeacher);
            ResultSet rs = stmt.executeQuery(); 
            ArrayList<Integer> listDay = new ArrayList<Integer>();
            while(rs.next()){
                Integer i = new Integer(rs.getInt("days"));
                listDay.add(i);
            }
            return listDay;
        }catch(SQLException e){
            System.err.println("Erro ao capturar o expediente!");
            System.out.println(e.getMessage());
        }
        return null;
    }
    
    public void removerExpedient(int idTeacher){
        try{
            sql = "DELETE FROM SAD.TEACHER_DAYS WHERE teacher=?";
            PreparedStatement stmt = connection.prepareStatement(sql);
            stmt.setInt(1, idTeacher);
            stmt.execute();
            stmt.close();
            System.err.println("Ele apagou!");
        }catch(SQLException e){
            System.err.println("Erro ao excluir o expediente!");
            System.out.println(e.getMessage());
        }
    }
    
    
    /**
     * Método que encerar a conexão com o banco.
     */
    public void closeConnection(){
        try{
            this.connection.close();
        }catch(SQLException e){
            System.err.println("Erro tentar fechar a conexão do expediente!");
            System.out.println(e.getMessage());
        }
    }
}
